//
//  HomeViewController.swift
//  HMXMeiTuan
//
//  Created by apple on 16/7/20.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {

    private lazy var catagoryVC : catagoryViewController = {
        
        let catagoryVC = catagoryViewController()
        catagoryVC.modalPresentationStyle = .Popover
        return catagoryVC
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setNavigationBar()
    }
}

extension HomeViewController{
    
    private func setNavigationBar(){
        
        //1.设置导航栏的背景颜色
        let navBar = navigationController?.navigationBar
        navBar?.setBackgroundImage(UIImage(named: "bg_navigationBar_normal"), forBarMetrics: .Default)
        
        //设置logo
        let logoItem = UIBarButtonItem(image: UIImage(named: "icon_meituan_logo"), style: .Plain, target: nil, action: nil)
        
        logoItem.enabled = false
        
        //设置分类栏
        let catagoryView = topButtonItem.viewForXib()
        catagoryView.settopView("icon_category_-1", iconNameHighLighted: "icon_category_highlighted_-1", title: "美团", subTitle: "全部分类")
        let catagoryBarItem = UIBarButtonItem(customView: catagoryView)
        catagoryView.titleBtn.addTarget(self, action: "catagoryItemClick:", forControlEvents: .TouchUpInside)
        
        
        //设置地区栏
        let regionView = topButtonItem.viewForXib()
        regionView.settopView("icon_district", iconNameHighLighted: "icon_district_highlighted", title: "广州", subTitle: "全部地区")
        let regionBarItem = UIBarButtonItem(customView: regionView)

        //设置排序栏
        let sortView = topButtonItem.viewForXib()
        sortView.settopView("icon_sort", iconNameHighLighted: "icon_sort_highlighted", title: "排序", subTitle: "默认排序")
        let sortBarItem = UIBarButtonItem(customView: sortView)
        
        navigationItem.leftBarButtonItems = [logoItem, catagoryBarItem, regionBarItem, sortBarItem]
        
    }
    
}

extension HomeViewController{
    
    @objc private func catagoryItemClick(btn: UIButton){
        
        catagoryVC.popoverPresentationController?.sourceView = btn
        catagoryVC.popoverPresentationController?.sourceRect = btn.bounds
        
        presentViewController(catagoryVC, animated: true, completion: nil)
    }
}
